//! $Id: NameSingletonContext.h 27 2011-09-16 13:28:08Z skagget77 $
//!
//! @file    NameSingletonContext.h
//! @author  Johan Andersson <skagget77@gmail.com>
//! @sate    Sun Oct 11 16:57 2009
//! @ingroup IoCpp
//!

#if !defined(IOCPP_CONFIG_NAMESINGLETONCONTEXT_H)
#define IOCPP_CONFIG_NAMESINGLETONCONTEXT_H

#include "IoCpp/Config/BindContext.h"
#include "IoCpp/Config/SingletonContext.h"

#include <boost/shared_ptr.hpp>
#include <string>

namespace IoCpp
{
   //!
   //! @author  Johan Andersson <skagget77@gmail.com>
   //! @date    Sun Oct 11 16:57 2009
   //! @ingroup IoCpp
   //!
   //! The NameSingletonContext class, part of the fluent interface used when
   //! binding a type to a provider. Can either associates a name with the
   //! provider being bound or bind the provider as a singleton.
   //!
   template<class T1>
   class NameSingletonContext
   {
   public :
      //!
      //! Creates a new instance of the NameSingletonContext class.
      //!
      //! @param context The BindContext holding the state of the fluent
      //!                interface while binding a type to a provider.
      //!
      NameSingletonContext( boost::shared_ptr<BindContext<T1> > context )
         : mContext(context) {
      }

      //!
      //! Associates a name with the provider being bound.
      //!
      //! @param name The name.
      //!
      SingletonContext<T1> WithName( const std::string& name )
      {
         mContext->SetName(name);

         return SingletonContext<T1>(mContext);
      }

      //!
      //! Binds the provider as a singleton. The singleton is created the first
      //! time that the bound type is injected.
      //!
      void AsSingleton() {
         mContext->SetAsSingleton();
      }

      //!
      //! Binds the provider as an eager singleton. The eager singleton is
      //! created at the time the type is bound.
      //!
      void AsEagerSingleton() {
         mContext->SetAsEagerSingleton();
      }

   private :
      boost::shared_ptr<BindContext<T1> > mContext;
   };
}

#endif   // IOCPP_CONFIG_NAMESINGLETONCONTEXT_H
